package com.tledu.production.controller.crafts;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.mapper.crafts.TechnologyMapper;
import com.tledu.production.model.crafts.TechnologyModel;
import com.tledu.production.model.system.SysUser;
import com.tledu.production.service.crafts.ITechnologyService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 赵若松
 */
@Controller
@RequestMapping("/technology")
@AllArgsConstructor
public class TechnologyController {
    final private ITechnologyService technologyService;

    @RequestMapping("/page")
    @ResponseBody
    //查询
    public AjaxResult<PageInfo<TechnologyModel>>page(HttpSession session, Integer pageNum, Integer pageSize, String condition, String conditionName){
        if (pageNum == null)pageNum = CommonConstant.PAGENUM;
        if (pageSize == null)pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum,pageSize);
        List<TechnologyModel> list = null;
        PageInfo<TechnologyModel> page = null;
        //分页
        if (conditionName==null||"".equals(conditionName)){
            list=technologyService.list();
            page=PageInfo.of(list);
        }else{
            //搜索
            list= technologyService.selectByName(condition,conditionName);
            page=PageInfo.of(list);
        }
        session.setAttribute("list",list);

        return new AjaxResult<>(page);
    }

    //删除
    @GetMapping("/delete")
    @ResponseBody
    public JSONObject del(String technologyId){
        int delete = technologyService.deleteByName(technologyId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success",delete);
        return jsonObject;
    }
    //添加
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult<Integer>add(TechnologyModel technologyModel){
        return new AjaxResult<>(technologyService.add(technologyModel));
    }
    //编辑
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult<Void>edit(TechnologyModel technologyModel){
        technologyService.edit(technologyModel);
        return new AjaxResult<>();
    }
    //批量删除
    @GetMapping("/del")
    @ResponseBody
    public AjaxResult batchDel(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            technologyService.deleteByName(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
}
